package stack

import "fmt"

/**
* 链式栈，即为只能在头部插入或者删除的单链表
 */

type Node struct {
	value int
	pNext *Node
}

func NewNode(value int) *Node {
	return &Node{
		value: value,
		pNext: nil,
	}
}

type ListStack struct {
	head *Node
	size int
}

func NewListStack() *ListStack {
	return &ListStack{
		head: NewNode(-1),
		size: 0,
	}
}

func (ls *ListStack) AddNode(value int) {
	node := NewNode(value)
	headPNext := ls.head.pNext
	ls.head.pNext = node
	node.pNext = headPNext

	ls.size++
}

func (ls *ListStack) DelNode() *Node {
	if ls.size <= 0 {
		return nil
	}

	delNode := ls.head.pNext
	ls.head.pNext = delNode.pNext

	delNode.pNext = nil
	ls.size--
	return delNode
}

func (ls *ListStack) Print() {
	tmpNode := ls.head.pNext
	for ;tmpNode != nil; {
		fmt.Printf(" %d", tmpNode.value)
		tmpNode = tmpNode.pNext
	}

	fmt.Println()
}
